/*
 * @File    :   index.js
 * @Time    :   2024/03/24 16:40:25
 * @Author  :   chakcy
 * @Email   :   947105045@qq.com
 * @description   :   窗口管理器
 */

const { BrowserWindow } = require('electron')
const isDev = require('electron-is-dev')

class WindowManager {
    constructor() {
        this.windows = [] // 存储窗口实例的数组  
        this.nextWindowId = 1
    }

    // 创建新窗口的方法  
    createWindow(render) {
        const win = new BrowserWindow({
            width: 1000,
            height: 1000,
            webPreferences: {
                nodeIntegration: true,
                webSecurity: false
            },
        });

        if (isDev) {
            win.loadURL(render['url'])
        } else {
            win.loadFile(render['path'])
        }
        win.webContents.openDevTools() // 打开调试工具  
        const winItem = {
            id: this.nextWindowId,
            win: win
        }
        this.windows.push(winItem) // 将新窗口添加到数组中  
        return winItem
    }

    // 关闭所有窗口的方法  
    closeAllWindows() {
        this.windows.forEach((win) => {
            win.close()
        })
        this.windows = [] // 清空窗口数组  
    }

}

module.exports = {
    WindowManager
}